using System;
using System.Collections.Generic;
using System.Text;
using Xyne.Objects;
using Xyne.Network;
using Xyne.Templates.PuzzlePieceTemplate;
using Microsoft.Xna.Framework.Net;
using System.Xml;

namespace Xyne.Actions
{
    /// <summary>
    /// Change's the player's alignment
    /// </summary>
    public class AlignmentChangeAction : Action
    {
        Dictionary<AlignmentEnum, float> modifications = new Dictionary<AlignmentEnum, float>();

        public Dictionary<AlignmentEnum, float> Modifications
        {
            get { return modifications; }
        }

        public AlignmentChangeAction()
        {
            Type = ActionTypes.AlignmentChange;
        }

        public AlignmentChangeAction(Action action)
            : base(action)
        {
            Type = ActionTypes.AlignmentChange;

            if (action is AlignmentChangeAction)
            {
                foreach (AlignmentEnum alignment in ((AlignmentChangeAction)action).Modifications.Keys)
                {
                    Modifications.Add(alignment, ((AlignmentChangeAction)action).Modifications[alignment]);
                }
            }
        }

        public override void Serialize(PacketWriter writer)
        {
            base.SerializeAction(writer);

            writer.Write(Modifications.Keys.Count);
            foreach (AlignmentEnum alignment in Modifications.Keys)
            {
                writer.Write((ulong)alignment);
                writer.Write(Modifications[alignment]);
            }
        }

        public static AlignmentChangeAction Deserialize(PacketReader reader)
        {
            AlignmentChangeAction action = new AlignmentChangeAction();
            action.DeserializeAction(reader);

            int numColors = reader.ReadInt32();
            for (int i = 0; i < numColors; i++)
            {
                action.Modifications.Add((AlignmentEnum)reader.ReadUInt64(), reader.ReadSingle());
            }

            return action;
        }

        public override void WriteXml(XmlWriter writer)
        {
            base.WriteXml(writer);

            writer.WriteStartElement("Modifications");
            foreach (AlignmentEnum alignment in modifications.Keys)
            {
                writer.WriteElementString(alignment.ToString(), modifications[alignment].ToString());
            }
            writer.WriteEndElement();
        }

        public override void ReadNode(XmlReader reader)
        {
            if (reader.LocalName == "Modifications")
            {
                while (reader.Read() && reader.LocalName != "Modifications")
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        try
                        {
                            AlignmentEnum alignment = (AlignmentEnum)Enum.Parse(typeof(AlignmentEnum), reader.LocalName,true);
                            Modifications.Add(alignment, float.Parse(reader.ReadString()));
                        }
                        catch (Exception) { }
                    }
                }
            }
        }
    }
}
